package ca.etsmtl.log660.controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import ca.etsmtl.log660.dao.DBConnection;
import ca.etsmtl.log660.dao.EntityFacade;
import ca.etsmtl.log660.dao.SQLBuilder;
import ca.etsmtl.log660.entity.Film;

public class ControllerFacade {
	
	private EntityFacade entityFacade;
	
	public ControllerFacade() {
		this.entityFacade = new EntityFacade();
	}

	public boolean logUserIn(String email, String password) {
		return ClientManager.getInstance().logUserIn(email, password);
	}
	
	public void rentMovie(Film movieToRent) {
		LocationManager locMan = new LocationManager();
		locMan.addLocation(movieToRent);
	}
	
	public String getCote(Long idFilm) {
		String cote = "";
		String query = SQLBuilder.getQueryCoteForFilm(String.valueOf(idFilm));
		try {
			Connection c = DBConnection.getConnection();
			Statement stm;
			stm = c.createStatement();
			ResultSet rs = stm.executeQuery(query);
			while (rs.next()) {
				cote = rs.getString("cote");
			}
			rs.close();
			stm.close();
			DBConnection.closeConnection();
		} catch (SQLException e) {
			System.out.println("film id : " + idFilm);
			System.err.println(e.getStackTrace());
		}
		return cote;
	}
	
	public ArrayList<Film> getSuggestions(Long idFilm) {
		ArrayList<String> filmsIds = new ArrayList<String>();
		ArrayList<Film> films = new ArrayList<Film>();
		
		Connection c;
		Statement stm;
		ResultSet rs;
		
		String query = SQLBuilder.getRecomandationQuery(
				String.valueOf(idFilm),
				String.valueOf(ClientManager.getInstance().getLoggedInClientId(true))
		);
		try {
			c = DBConnection.getConnection();
			stm = c.createStatement();
			rs = stm.executeQuery(query);
			while (rs.next()) {
				filmsIds.add(rs.getString("filmcons"));
			}
			rs.close();
			stm.close();
			DBConnection.closeConnection();
		} catch (SQLException e) {
			System.out.println("film id : " + idFilm);
			System.err.println(e.getStackTrace());
		}
		
		Film currentFilm;
		c = DBConnection.getConnection();
		for (String filmId : filmsIds) {
			currentFilm = this.entityFacade.getFilmById(Long.valueOf(filmId));
			
			query = SQLBuilder.getCorelationFactorQuery(
					String.valueOf(idFilm),
					String.valueOf(currentFilm.getIdFilm())
			);
			try {
				stm = c.createStatement();
				rs = stm.executeQuery(query);
				while (rs.next()) {
					currentFilm.setCorrelation(rs.getString("r"));
				}
				rs.close();
				stm.close();
			} catch (SQLException e) {
				System.err.println(e.getStackTrace());
			}
			films.add(currentFilm);
		}
		DBConnection.closeConnection();
		
		return films;
//		return MockFilm.getMockedFilms();
	}
	
	public List<Film> searchMovie(
			List<String> titlesFilters,
			String startYearFilter,
			String endYearFilter,
			List<String> prodCountriesFilters,
			List<String> langagesFilters,
			List<String> genresFilters,
			List<String> realisatorsFilters,
			List<String> actorsFilters
	) {
		return this.entityFacade.searchMovie(
			titlesFilters,
			startYearFilter,
			endYearFilter,
			prodCountriesFilters,
			langagesFilters,
			genresFilters,
			realisatorsFilters,
			actorsFilters
		);
	}
}
